package com.ysx.oa.holiday.service.Impl;

import com.ysx.oa.clazz.bean.ClazzStudentTeacherBean;
import com.ysx.oa.clazz.dao.ClazzStudentTeacherDao;
import com.ysx.oa.holiday.bean.HolidayBean;
import com.ysx.oa.holiday.dao.HolidayDao;
import com.ysx.oa.holiday.service.HolidayService;
import com.ysx.oa.user.bean.UserBean;
import com.ysx.oa.user.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 王伟雄
 * @Date: 2021/11/21 17:38
 */
@Service
public class HolidayServiceImpl implements HolidayService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private HolidayDao dao;

    @Autowired
    private ClazzStudentTeacherDao clazzStudentTeacherDao;

    @Override
    public Integer addByStudent(HolidayBean holidayBean) {
        UserBean userBean=new UserBean();
        userBean.setId(holidayBean.getStudentId());
        // 通过前端传来的名字查到用户id，用户id与学生id相同
        UserBean studentBean = userDao.selectOne(userBean);
        if(studentBean==null){
            return 0;
        }
        // 把获取到的学生id放入bean中
        holidayBean.setFlag(900100);
        return dao.insert(holidayBean);
    }

    @Override
    public Integer deleteByStudent(HolidayBean holidayBean) {
        if(dao.select(holidayBean).size()==0){
            return 0;
        }
        return dao.delete(holidayBean);
    }

    @Override
    public Integer updateByTeacher(HolidayBean holidayBean) {
        return dao.updateByPrimaryKeySelective(holidayBean);
    }


    @Override
    public List<HolidayBean> findByStudent(Integer id) {

        return dao.findByStudent(id);

    }

    @Override
    public List<HolidayBean> findByTeacher(Integer id,Integer flag) {
        // 通过用户id找到所在班级
        ClazzStudentTeacherBean clazzStudentTeacherBean=new ClazzStudentTeacherBean();
        clazzStudentTeacherBean.setUserId(id);
        Integer clazzId = clazzStudentTeacherDao.selectOne(clazzStudentTeacherBean).getClazzId();
        List<HolidayBean> holidayBeans = dao.findByTeacher(clazzId,flag);
        // 通过班级，查出班级所在的学生的请假情况
        return holidayBeans;
    }




}
